package com.focus.xyz.core.pages;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.utils.Constants;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by chenmeng on 2016/4/18.
 */
public abstract class BaseForm extends BasePage{
    protected Action action;
    private int index = 0;
    public BaseForm(WebDriver driver,Action action) {
        super(driver,action);
        this.action = action;
    }

    protected abstract List<WebElement> getAllElements() throws Exception;

    public void bind(Object bean) throws Exception {
        if (bean == null) {
            throw new Exception("绑定到表单的对象为空");
        }
        Method[] methods = bean.getClass().getDeclaredMethods();
        for (WebElement e : getAllElements()) {
            for (Method m : methods) {
                String methodName = m.getName();
                String v = StringUtils.isNotBlank(e.getAttribute(Constants.ATTR_ID)) ? e.getAttribute(Constants.ATTR_ID) : e.getAttribute(Constants.ATTR_NAME);
                if (StringUtils.isNotBlank(methodName) && methodName.startsWith("get") && methodName.substring(3).equalsIgnoreCase(v)) {
                    Object beanValue = m.invoke(bean, new Object[]{});
//                    String eValue = e.getAttribute(Constants.ATTR_VALUE);
                    //如果bean对象中的属性没有值，则不更新。
                    if (null == beanValue) {
                        break;
                    }
                    if (Constants.TAG_SELECT.equals(e.getTagName())) {
                        /*//如果bean对象中属性有值，并且元素中没值才更新
                        if(StringUtils.isNotBlank(eValue)){
                            break;
                        }*/
                        action.select(e, beanValue.toString());
                        break;
                    } else if (Constants.TAG_INPUT.equals(e.getTagName())) {
                        if (Constants.TYPE_TEXT.equals(e.getAttribute(Constants.ATTR_TYPE))) {
                            /*//如果bean对象中属性有值，并且元素中没值才更新
                            if(StringUtils.isNotBlank(eValue)){
                                break;
                            }*/
                            if(e.isEnabled()){
                                action.inputText(e, beanValue.toString());
                            }
                            break;
                        } else if (Constants.TYPE_CHECKBOX.equals(e.getAttribute(Constants.ATTR_TYPE))) {
                            if ("Y".equals(beanValue)) {
                                action.selectCheckBox(e);
                            } else {
                                action.deselectCheckBox(e);
                            }
                            break;
                        } else if (Constants.TYPE_RADIO.equals(e.getAttribute(Constants.ATTR_TYPE))) {
                            if ("Y".equals(beanValue)) {
                                action.click(e);
                            }
                            break;
                        } else if (Constants.TYPE_FILE.equals(e.getAttribute(Constants.ATTR_TYPE))) {
                            //上传组件
                            //TODO
                            break;
                        }
                    } else if (Constants.TAG_TEXTAREA.equals(e.getTagName())) {
                       /* //如果bean对象中属性有值，并且元素中没值才更新
                        eValue = e.getText();
                        if(StringUtils.isNotBlank(eValue)){
                            break;
                        }*/
                        action.inputText(e, beanValue.toString());
                        break;
                    }
                } else {
                    continue;
                }
            }
        }

    }

    /*
    * 从form中获取数据到bean对象
    * */
    public Object getBean(WebElement form) throws Exception {
        return null;
    }


    protected void bind1(WebElement form, Object bean) throws Exception {
        if (bean == null) {
            throw new Exception("绑定到表单的对象为空");
        }
        Method[] methods = bean.getClass().getDeclaredMethods();
        for (WebElement e : getAllElements()) {
            for (Method m : methods) {
                String methodName = m.getName();
                String v = StringUtils.isNotBlank(e.getAttribute(Constants.ATTR_ID)) ? e.getAttribute(Constants.ATTR_ID) : e.getAttribute(Constants.ATTR_NAME);
                if (StringUtils.isNotBlank(methodName) && methodName.startsWith("get")) {
                    Object beanValue = null;
                    v.startsWith(methodName.substring(3));
                    String newV = null;
                    if(v.startsWith(methodName.substring(3))){
                        beanValue = m.invoke(bean, new Object[]{});
                        //如果bean对象中的属性没有值，则不更新。
                        if (null == beanValue) {
                            break;
                        }
                        newV = methodName.substring(3) + "_" + index + "_" + beanValue;
                        if(newV.equalsIgnoreCase(v)){
                            if (Constants.TAG_SELECT.equals(e.getTagName())) {
                                action.select(e, beanValue.toString());
                                break;
                            } else if (Constants.TAG_INPUT.equals(e.getTagName())) {
                                if (Constants.TYPE_TEXT.equals(e.getAttribute(Constants.ATTR_TYPE))) {
                                    action.inputText(e, beanValue.toString());
                                    break;
                                } else if (Constants.TYPE_CHECKBOX.equals(e.getAttribute(Constants.ATTR_TYPE))) {
                                    if ("Y".equals(beanValue)) {
                                        action.selectCheckBox(e);
                                    } else {
                                        action.deselectCheckBox(e);
                                    }
                                    break;
                                } else if (Constants.TYPE_RADIO.equals(e.getAttribute(Constants.ATTR_TYPE))) {
                                    action.click(e);
                                    break;
                                } else if (Constants.TYPE_FILE.equals(e.getAttribute(Constants.ATTR_TYPE))) {
                                    //上传组件
                                    //TODO
                                    break;
                                }
                            } else if (Constants.TAG_TEXTAREA.equals(e.getTagName())) {
                                action.inputText(e, beanValue.toString());
                                break;
                            }

                        }
                    }
                } else {
                    continue;
                }
            }
        }

    }
}
